﻿using System;
using System.Windows.Forms;
using TestSystem.Model;
using TestSystem.Ui.Controls;
using TestSystem.Ui.Forms;

namespace TestSystem.Ui
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private void OnButtonExitClick(object sender, EventArgs e)
        {
            if (Service.ApplicationCanBeClosed)
                Application.Exit();
        }

        private void OnCreateFolderClick(object sender, EventArgs e)
        {
            CreateNewFolder();
        }

        private void OnCreateToolClick(object sender, EventArgs e)
        {
            CreateNewTool();
        }

        private void MainFormLoad(object sender, EventArgs e)
        {
            InitializeFormData();
            tsbCreateTool.Enabled = false;
        }

        private void OnTreeViewDataSelectinChanged(object sender, TreeViewEventArgs e)
        {
            var folderSelected = treeViewData.SelectedNode is FolderTreeNode;
            var toolSelected = treeViewData.SelectedNode is ToolTreeNode;
            
            tsbCreateTool.Enabled = folderSelected || toolSelected;
            tsbCreateFolder.Enabled = folderSelected;
            tsbRemove.Enabled = folderSelected || toolSelected;
            buttonAdd.Enabled = treeViewData.SelectedNode.Level == 2 && treeViewData.Nodes.Count != 0;
            buttonDelete.Enabled = treeViewData.SelectedNode.Level == 2 && treeViewData.Nodes.Count != 0;
            buttonEdit.Enabled = treeViewData.SelectedNode.Level == 2 && treeViewData.Nodes.Count != 0;

        }

        private void OnDeleteNode(object sender, EventArgs e)
        {
            DeleteSelectedNode();
        }

        private void treeViewData_DoubleClick(object sender, EventArgs e)
        {
            if (!(treeViewData.SelectedNode is FrequencyTreeNode)) return;
            labelView.Text = treeViewData.SelectedNode.Parent.Text + @" (" + treeViewData.SelectedNode.Text + @")";
            ShowCurrentData();
        }

        private void treeViewDataKeyUp(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode != Keys.F2) || (treeViewData.SelectedNode == null)) return;
            var node = treeViewData.SelectedNode;
            if (node is FolderTreeNode) RenameSelectedFolder();
            else if(node is ToolTreeNode) RenameSelectedTool();
        }

       private void RenameItemClick(object sender, EventArgs e)
        {

            RenameSelectedFolder();
           
        }

        private void RenameToolClick(object sender, EventArgs e)
        {
            RenameSelectedTool();
        }

        private void DeleteItemClick(object sender, EventArgs e)
        {
            DeleteSelectedNode();
        }

        private void AddItemClick(object sender, EventArgs e)
        {
            CreateNewFolder();
        }

        private void DeleteToolClick(object sender, EventArgs e)
        {
            DeleteSelectedNode();
        }

        private void AddToolClick(object sender, EventArgs e)
        {
            CreateNewTool();
        }

        private void ButtonAddClick(object sender, EventArgs e)
        {
            if (!(treeViewData.SelectedNode is FrequencyTreeNode)) return;
            AddNewQuote();
        }

        private void buttonEditClick(object sender, EventArgs e)
        {
            if (DataGrid.SelectedRows.Count != 1) return;
            EditSelectedQuote();
        }

        private void buttonDeleteClick(object sender, EventArgs e)
        {
            if (DataGrid.SelectedRows.Count == 0) return;
            if (MessageBox.Show(@"Are you sure want to delete this string(s)?", @"Test System", MessageBoxButtons.OKCancel,
               MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification, false) != DialogResult.OK) return;
            DeleteSelectedQuotes();
        }

        private void buttonImportDataClick(object sender, EventArgs e)
        {
            var importform = new ImportForm();
            importform.ShowDialog();
        }

        private void MainFormActivated(object sender, EventArgs e)
        {
            tsbCreateTool.Enabled = treeViewData.SelectedNode != null;
        }

        
    }
}
